package com.xinchuang.service.targetSystem.Impl.damage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinchuang.comment.R;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.dto.HsCalculateStandardDto;
import com.xinchuang.entity.targetSystem.damage.HurtBasic;
import com.xinchuang.entity.targetSystem.damage.HurtBasicCorrelation;
import com.xinchuang.mapper.targetSystem.damage.HurtBasicCorrelationMapper;
import com.xinchuang.mapper.targetSystem.damage.HurtBasicMapper;
import com.xinchuang.service.targetSystem.damage.IHsCalculateStandardService;
import com.xinchuang.vo.HsCalculateStandardVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * HS计算标准 服务实现类
 * </p>
 *
 * @author zhb
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class HsCalculateStandardServiceImpl extends ServiceImpl<HurtBasicMapper, HurtBasic> implements IHsCalculateStandardService {

    private final DictionaryTableServiceImpl dictionaryTableService;

    private final HurtBasicMapper hurtBasicMapper;

    private final HurtBasicCorrelationMapper hurtBasicCorrelationMapper;


    @Override
    public R selectEntities(int pageNum, int pageSize, String targetTypeId,String hurtLevel,String destroyLevel) {
        try {
            PageHelper.startPage(pageNum, pageSize);
            List<HsCalculateStandardDto> dtos = hurtBasicMapper.selectEntities(targetTypeId,hurtLevel,destroyLevel);
            PageInfo<HsCalculateStandardDto> page = new PageInfo<>(dtos);
            page.setTotal(page.getList().size());
            //重写定义分页插件返回参数
            Map<String, Object> map = new HashMap<>();
            map.put("dataList", page.getList());
            map.put("total", page.getTotal());
            return R.data(map, "获取成功");
        } catch (Exception e) {
            log.info("获取HS计算标准列表失败:{}", e.toString(), e);
        }
        return R.fail("获取失败");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result addOrUpdateEntity(HsCalculateStandardVo form) {
        if (StringUtils.isBlank(form.getDestroyLevel())) {
            return ResultUtil.error(500, "毁伤等级不能为空");
        }
        if (StringUtils.isBlank(form.getHurtLevel())) {
            return ResultUtil.error(500, "破坏等级不能为空");
        }
        try {
            if (StringUtils.isNotBlank(form.getId())) {
                hurtBasicCorrelationMapper.delete(new QueryWrapper<HurtBasicCorrelation>().lambda()
                        .eq(HurtBasicCorrelation::getHurtBasicId, form.getId()));
                HurtBasic hurtBasic = setHurtBasic(form.getId(), form);
                hurtBasicMapper.updateById(hurtBasic);
                form.getHsTargetType().forEach(x -> {
                    HurtBasicCorrelation hurtBasicCorrelation = setHurtBasicCorrelation(form.getId(), x.getTypeId());
                    hurtBasicCorrelationMapper.insert(hurtBasicCorrelation);
                });
                return ResultUtil.success(null, "修改成功");
            } else {
                String basicId = UUID.randomUUID().toString().replace("-", "");
                HurtBasic hurtBasic = setHurtBasic(basicId, form);
                hurtBasic.setDataStatus(0);
                hurtBasicMapper.insert(hurtBasic);
                form.getHsTargetType().forEach(x -> {
                    HurtBasicCorrelation hurtBasicCorrelation = setHurtBasicCorrelation(basicId, x.getTypeId());
                    hurtBasicCorrelationMapper.insert(hurtBasicCorrelation);
                });
                return ResultUtil.success(null, "新增成功");
            }
        } catch (Exception e) {
            log.info("增加或修改HS计算标准失败:{}", e.toString(), e);
        }

        return ResultUtil.error(500, "增加或修改失败");
    }

    private HurtBasicCorrelation setHurtBasicCorrelation(String basicId, String typeId) {
        HurtBasicCorrelation hurtBasicCorrelation = new HurtBasicCorrelation();
        String correlationId = UUID.randomUUID().toString().replace("-", "");
        hurtBasicCorrelation.setId(correlationId);
        hurtBasicCorrelation.setHurtBasicId(basicId);
        hurtBasicCorrelation.setTargetTypeSubId(typeId);
        hurtBasicCorrelation.setDataStatus(0);
        return hurtBasicCorrelation;
    }

    private HurtBasic setHurtBasic(String basicId, HsCalculateStandardVo form) {
        HurtBasic hurtBasic = new HurtBasic();
        hurtBasic.setId(basicId);
        hurtBasic.setTargetTypeId(form.getTargetTypeId());
        hurtBasic.setDestroyLevel(form.getDestroyLevel());
        hurtBasic.setHurtLevel(form.getHurtLevel());
        hurtBasic.setHurtInfo(form.getHurtInfo());
        return hurtBasic;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R deltEntities(String ids, boolean deleteStatus) {
        try {
            if (deleteStatus) {
                hurtBasicMapper.deleteById(ids);
                hurtBasicCorrelationMapper.delete(new QueryWrapper<HurtBasicCorrelation>().lambda()
                        .in(HurtBasicCorrelation::getHurtBasicId, ids));
            } else {
                String param = "'" + String.join("','", ids) + "'";
                hurtBasicMapper.deleteBatchIdsWithLogic(param);
                hurtBasicCorrelationMapper.deleteWithLogic(param);
            }
            return R.success("删除成功");
        } catch (Exception e) {
            log.info("删除HS计算标准失败:{}", e.toString(), e);
        }
        return R.fail("删除失败");
    }
}
